<?php
namespace app\store\controller\parent;


use app\store\controller\Base;
use app\store\controller\Weixin;
use think\Db;
header("Content-type:text/html;charset=utf-8");

class Article extends Base
{

    /*
     * 文章推荐
     * wangtiantian
     */
    public function article(){

        $res = Db::name('article_sort')->where('is_del',0)->select();

        $this->assign('res',$res);
        $this->assign('shop_id',$this->shop_id);

        return $this->fetch();
    }

    /*
     * 文章列表
     * wangtiantian
     */
    public function artlist(){


        if(request()->isAjax()){
            //获取文章分类id
            $sort_id = input('sort_id');

            //通过sort_id 到文章表找相应类别的文章
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 5;

            $res = Db::name('article')
                ->where('status',1)
                ->where('sort_id',$sort_id)
                ->page($page,$limit)
                ->select();

            foreach ($res as $index => $re) {
                $res[$index]['create_time'] =   date('Y-m-d',$re['create_time']);
//                $res[$index]['thumb'] = 'public/'.  $re['thumb'];
            }

            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res),  'data' => $res]);

        }

        $sort_id    = input('id');
        $title      = Db::name('article_sort')->where('id',$sort_id)->value('sort');
        $this->assign('title',$title);
        $this->assign('sort_id',input('id'));
        $this->assign('shop_id',$this->shop_id);
        return  $this->fetch();

    }


    /*
     * 文章详情
     * wangtiantian
     */

    public function detail(){

        // 点赞提交
        if(request()->isAjax()){

            $id     = input('id');   // 获取文章id
            $read   = input('praise'); // 获取原点赞数
            $readed = $read+1;          // 点赞数+1

            // 更新点赞数量
           $res =    db('article')->where('status',1)->where('id',$id)->update(['praise'=>$readed]);
           // 点赞记录表插入一条记录
           Db::name('article_praise')->insert(['article_id'=>$id,'uid'=>$this->uid,'status'=>1]);

            if ($res){
                $this->success('点赞成功');
            }else{
                $this->error('点赞失败');
            }

        }
        // 获取文章id
        $article_id = input('id');

        $res = Db::name('article')->where('status',1)->where('id',$article_id)->find();

        $res['hit']+= 1;   // 增加点击量

             db('article')->where('status',1)->where('id',$article_id)->update(['hit'=>$res['hit']]);

        $res['create_time'] =date('Y-m-d',$res['create_time']);
        // 查询是否已点赞
        $a= Db::name('article_praise')->where('article_id',$article_id)->where('uid',$this->uid)->value('status');

        $this->assign('status',$a);
        $this->assign('res',$res);

        return $this->fetch();
    }



}
